
auto.waitFor();

var w = floaty.window(
    <vertical alpha="1">
        <button id="up" w='50' text="开始" />
        <button id="delayBut" text="延时" />
        <button id="hBut" text="横向点" />
    </vertical>
);
//点击延时
var delayTime = 300;

var q = floaty.rawWindow(
    //<vertical bg="#ff0000">
        <button id="but" w="40" h="40" alpha="0.7" text="*" bg="#888888" />
     //</vertical >
);
//y左边从导航栏开始，真正坐标需要加上导航栏高度 118
var statusheight = getStatusBarHeightCompat();

q.setPosition(100, 100);
//悬浮框宽高
var qWidth = null;
var qHigth = null;
//悬浮框左上坐标
this.TX = 0;
this.TY = 0;
//按钮事件坐标
this.Tx = 0;
this.Ty = 0;
var cx = 0;
var cy = 0;

q.but.setOnTouchListener(new android.view.View.OnTouchListener((view, event) => {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            if (qWidth == null) {
                qWidth = q.but.getWidth() / 2;
                qHigth = q.but.getHeight() / 2;
                q.setSize(q.but.getWidth() +10, q.but.getHeight()+10)
            }
            this.Tx = event.getRawX();
            this.Ty = event.getRawY();
            this.TX = q.getX();
            this.TY = q.getY();
            // log('按下坐标', Tx, Ty);
            // log('悬浮框坐标', TX, TY)
            break;
        case event.ACTION_MOVE:
            //移动的距离
            var sx = event.getRawX() - this.Tx;
            var sy = event.getRawY() - this.Ty;
            //初始位置+移动距离
            q.setPosition(this.TX + sx, this.TY + sy);
            break;
        case event.ACTION_UP:
            // log('放开坐标', event.getRawX(), event.getRawY());
            // log('放开悬浮框坐标', q.getX(), q.getY())
            cx = q.getX() + qWidth;
            cy = q.getY() + qHigth + statusheight;
            // log('点击位置', cx, cy);
            break;
    };
    return true;
}));

var btFlag = 1;
var htFlag = 1;
// w.setSize(240, 440);
// w.setTouchable(true);
w.setAdjustEnabled(true);
w.setPosition(100,  device.height - 800);
w.exitOnClose();

q.setSize(240, 440);
q.exitOnClose();
setInterval(() => { }, 1000);

w.up.click(function () {
    btFlag = !btFlag;
    if (btFlag) {
        w.up.setText('开始');
        q.setTouchable(true);
    } else {
        w.up.setText('停止');
        // log('开始点击')
        q.setTouchable(false);
        threads.start(function () {
            while(!btFlag) {
                click(cx, cy);
                sleep(delayTime);
            }
        });
    }
});
w.delayBut.click(function () {
    threads.start(function () {
        delayTime = rawInput("请输入延时，单位毫秒"); 
    });
});

var clickPos = [80, 160, 240, 320, 400, 480, 560, 640, 720, 800, 880, 960, 1040];
w.hBut.click(function () {
    htFlag = !htFlag;
    if (htFlag) {
        w.hBut.setText('横向点');
        q.setTouchable(true);
    } else {
        w.hBut.setText('停止');
        // log('开始点击')
        q.setTouchable(false);
        threads.start(function () {
            while(!htFlag) {
                for (let key of clickPos) {
                    click(key, cy);
                    sleep(delayTime)
                }
            }
        });
    }
});


function getStatusBarHeightCompat() {
    let result = 0
    let resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android")
    if (resId > 0) {
        result = context.getResources().getDimensionPixelOffset(resId)
    }
    if (result <= 0) {
        result = context.getResources().getDimensionPixelOffset(R.dimen.dimen_25dp)
    }
    return result
}
